﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Conditional Exercise 6</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Calculates and prints real roots of a quadratic equation (a*x<sup>2</sup> + b*x + c = 0)</h1>
    <div>
        <label for="num1">a:</label>
        <input type="number" id="num1" />
        <label for="num2">b:</label>
        <input type="number" id="num2" />
        <label for="num3">c:</label>
        <input type="number" id="num3" />
        <button onclick="execute()">Execute</button>
    </div>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function execute()
        {
            var A = jsConsole.readFloat("#num1");
            var B = jsConsole.readFloat("#num2");
            var C = jsConsole.readFloat("#num3");
            var D = Math.pow(B,2) - (4*A*C);

            if (D < 0)
            {
                jsConsole.writeLine("No real roots");
            }
            else if (0 == D)
            {
                var root1 = (-B) / (2 * A);
                jsConsole.writeLine("One real root: " + root1);
            }
            else // D > 0
            {
                var root1 = ((-B) - Math.sqrt(D)) / (2 * A);
                var root2 = ((-B) + Math.sqrt(D)) / (2 * A);
                jsConsole.writeLine("Two real roots: " + root1 + ", " + root2);
            }
        }
    </script>
</body>
</html>
